import requests
import re
import sqlite3

headers = {
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.46'
}
conn = sqlite3.connect('daxue.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS daxue (  
                    id INTEGER PRIMARY KEY,  
                    name text,
                    score text  
                )''')
url = 'https://www.shanghairanking.cn/_nuxt/static/1697106492/rankings/bcur/2021/payload.js'
response = requests.get(url=url,headers=headers)
data = response.text
namelist = re.findall("univNameCn:(.*?),univNameEn:",data)
scorelist = re.findall("score:(.*?),",data)
print("{:<10}{:<10}{:<10}".format("排名","学校","总分"))
x = 0
for i,j in zip(namelist,scorelist):
    x=x+1
    print("{:<10}{:<10}{:<10}".format(x,i,j))
    row = (x,i,j)
    cursor.execute('INSERT INTO daxue VALUES (?, ?, ?)', row)
conn.commit()  # 提交更改并关闭连接
conn.close()